/**
 * Copyright © 2015 eqxiu.com 北京中网易企秀科技有限公司  All rights reserved.
 */
package com.eqxiu.ieditor.api.impl;

import com.eqxiu.ieditor.api.IElementTagServiceApi;
import com.eqxiu.ieditor.dao.ElementTagMapper;
import com.eqxiu.ieditor.dto.ElementTagDTO;
import com.eqxiu.ieditor.entity.ElementTag;
import com.eqxiu.ieditor.entity.ElementTagExample;
import com.eqxiu.ieditor.entity.ElementTagKey;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description //TODO 设计说明
 * @author ranger
 * @date Jun 1, 2017
 */
@Service
public class ElementTagServiceApiImpl implements IElementTagServiceApi {

	@Autowired
	private ElementTagMapper elementTagMapper;

	@Override
	public int deleteByPrimaryKey(Long pageId, Long elementId) {
		ElementTagKey key = new ElementTagKey();
		key.setPageId(pageId);
		key.setElementId(elementId);
		return elementTagMapper.deleteByPrimaryKey(key);
	}

	@Override
	public int insert(ElementTagDTO record) {
		ElementTag tag = new ElementTag();
		BeanUtils.copyProperties(record, tag);
		return elementTagMapper.insert(tag);
	}

	@Override
	public int insertSelective(ElementTagDTO record) {
		ElementTag tag = new ElementTag();
		BeanUtils.copyProperties(record, tag);
		return elementTagMapper.insertSelective(tag);
	}

	@Override
	public Optional<List<ElementTagDTO>> selectByExample(Integer tagId) {
		ElementTagExample example = new ElementTagExample();
		example.createCriteria().andTagIdEqualTo(tagId);
		List<ElementTag> tags = elementTagMapper.selectByExample(example);

		if (Objects.nonNull(tags)) {
			return Optional.of(
					tags.stream().map(
							one -> {
								ElementTagDTO dto = new ElementTagDTO();
								BeanUtils.copyProperties(one, dto);
								return dto;
							}
					).collect(Collectors.toList())
			);
		}
		return Optional.empty();
	}

	@Override
	public ElementTagDTO selectByPrimaryKey(Long pageId, Long elementId) {
		ElementTagKey key = new ElementTagKey();
		key.setPageId(pageId);
		key.setElementId(elementId);
		ElementTag one = elementTagMapper.selectByPrimaryKey(key);
		ElementTagDTO dto = new ElementTagDTO();
		BeanUtils.copyProperties(one, dto);
		return dto;
	}

}
